import { computed, type ComputedRef, type Ref } from 'vue'
import { PhoenixPlatformEnum, PlatformEnum } from '@/core/enums/platform'
import { union } from 'lodash-es'
import { VersionTypeEnum } from '@/core/enums/type'
import type { BwPriceAskWords } from '@/services/price-sheet/getBwPriceAskWords'
import type { PhoenixRegionCheckResult } from '@/services/price-sheet/getPhoenixRegionCheckResult'
import type { BwPriceAskParams } from '@/services/price-sheet/getBwPriceAskWords'
import { ProductEnum } from '@/core/enums/platform'

export function usePriceSold(
  bwPriceInfo: BwPriceAskWords,
  versionType: ComputedRef<VersionTypeEnum>,
  phoenixChecker: Ref<Record<string, PhoenixRegionCheckResult>>,
  BwFormFields: Ref<BwPriceAskParams>,
  type: ProductEnum
) {
  const serviceAreas = computed(() => {
    return BwFormFields.value.serviceAreas
  })
  // pc端已售词
  const pcSold = computed(() => {
    const pcList = bwPriceInfo.wordAskWords.filter((word) => word.clientType === PlatformEnum.PC)

    const soldOfAll = union(
      pcList.reduce<string[]>((data, next) => {
        let soldAreas: undefined | string
        const { Normal, Cheap } = VersionTypeEnum

        // 版本为普通版
        if (versionType.value === Normal) {
          // ||undefined是为了把空字符串判定为undefined

          soldAreas = next.standardVersionSoldAreas || undefined
        }
        // 版本为特惠版
        if (versionType.value === Cheap) {
          // ||undefined是为了把空字符串判定为undefined
          soldAreas = next.cheapVersionSoldAreas || undefined
        }
        // 优选版不调老标王2.0接口
        const userId = localStorage.getItem('token')?.split('&')[1]

        if (
          (localStorage.getItem('code') != 1 && type === ProductEnum.BWYXB) ||
          type === ProductEnum.CBYXB ||
          userId == '1015431"'
        ) {
          return data.concat(soldAreas?.split(',') ?? []).concat([])
        } else {
          // 查询老标王2.0已售词
          const checkerResult = phoenixChecker.value[`${next.name}|${PhoenixPlatformEnum.PC}`]
          // 合并老标王2.0已售词
          return data.concat(soldAreas?.split(',') ?? []).concat(checkerResult?.regionList ?? [])
        }
      }, [])
    )
    if (soldOfAll.some((area) => ['全国', '中国'].includes(area))) {
      return true
    }
    if (serviceAreas.value.includes('全国') && soldOfAll.length > 0) {
      return true
    }
    return serviceAreas.value.some((area) => soldOfAll.includes(area))
  })

  // 移动端已售词
  const mobileSold = computed(() => {
    const mobileList = bwPriceInfo.wordAskWords.filter((word) => word.clientType === PlatformEnum.MOBILE)

    const soldOfAll = union(
      mobileList.reduce<string[]>((data, next) => {
        let soldAreas
        const { Normal, Cheap } = VersionTypeEnum

        // 版本为普通版
        if (versionType.value === Normal) {
          // ||undefined是为了把空字符串判定为undefined
          soldAreas = next.standardVersionSoldAreas || undefined
        }

        // 版本为特惠版
        if (versionType.value === Cheap) {
          // ||undefined是为了把空字符串判定为undefined
          soldAreas = next.cheapVersionSoldAreas || undefined
        }
        // 优选版不调老标王2.0接口
        const userId = localStorage.getItem('token')?.split('&')[1]

        if (type === ProductEnum.CBYXB || type === ProductEnum.BWYXB || userId == '1015431"') {
          return data.concat(soldAreas?.split(',') ?? [])
        } else {
          // 查询老标王2.0已售词
          const checkerResult = phoenixChecker.value[`${next.name}|${PhoenixPlatformEnum.MOBILE}`]

          // 合并老标王2.0已售词
          return data.concat(soldAreas?.split(',') ?? []).concat(checkerResult?.regionList ?? [])
        }
      }, [])
    )

    if (soldOfAll.some((area) => ['全国', '中国'].includes(area))) {
      return true
    }
    if (serviceAreas.value.includes('全国') && soldOfAll.length > 0) {
      return true
    }

    return serviceAreas.value.some((area) => soldOfAll.includes(area))
  })

  return {
    pcSold,
    mobileSold
  }
}
